package com.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.R;
import com.app.model.bean.Bike;
import com.app.model.bean.Record;
import com.app.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 */

public class RecordAdapter extends RecyclerView.Adapter {

    private static final String LOG_TAG = RecordAdapter.class.getSimpleName();

    private Context mContext;

    private List<Record> items = new ArrayList<>();

    private final int TYPE_NORMAL = 0;

    private final int TYPE_NO_CONTENT = 1;


    public RecordAdapter(Context context) {
        mContext = context;
    }

    private void initTestData() {
        for (int i = 0; i < 10; i++) {
            Record record = new Record();
            Bike bike = new Bike();
            bike.setNum("24471763");
            record.setBike(bike);
            record.setTime(System.currentTimeMillis());
            record.setAction(Record.ACTION_GET_PWD);
            items.add(record);
        }
    }

    public void setItems(List<Record> records) {
        this.items = records;
        if (items.size() < 1) {
            initTestData();
        }
        notifyDataSetChanged();
    }

    public void addItems(List<Record> records) {
        items.addAll(records);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (items.size() > 0) {
            return TYPE_NORMAL;
        } else {
            return TYPE_NO_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_NORMAL) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_record, parent, false);
            MViewHolder viewHolder = new MViewHolder(itemView);
            return viewHolder;
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.no_content, parent, false);
            NViewHolder viewHolder = new NViewHolder(itemView);
            return viewHolder;
        }
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (items.size() > 0) {
            MViewHolder viewHolder = (MViewHolder) holder;
            final Record record = items.get(position);
            viewHolder.tvNum.setText(record.getBike().getNum());
            viewHolder.tvTime.setText(TimeUtil.getFormatTime(record.getTime(), TimeUtil.FORMAT_TIME_SECONDS));
            switch (record.getAction()) {
                case Record.ACTION_GET_PWD:
                    viewHolder.tvType.setText("获取解锁码");
                    break;
                case Record.ACTION_SHARE_PWD:
                    viewHolder.tvType.setText("分享解锁码");
                    break;
                case Record.ACTION_WRONG_PWD:
                    viewHolder.tvType.setText("解锁码错误上报");
                    break;
                default:
                    break;
            }

        }
    }

    @Override
    public int getItemCount() {
        if (items.size() > 0) {
            return items.size();
        } else {
            return 1;
        }
    }


    class MViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tvType)
        TextView tvType;
        @BindView(R.id.tvNum)
        TextView tvNum;
        @BindView(R.id.tvTime)
        TextView tvTime;

        public MViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class NViewHolder extends RecyclerView.ViewHolder {


        public NViewHolder(View itemView) {
            super(itemView);
        }
    }
}
